<?
session_start();
include('conn.php');

$custid = $_SESSION['custid'];
$custtype = $_SESSION['custtype'];

 
$qry1 = "select c.custid, c.custdesc as custdesc, c.custtype as custtype, t.status, IF( (sum(t.debitamt) - sum(t.creditamt)) is NULL,0,(sum(t.debitamt) - sum(t.creditamt)) ) as outstdamt from customermast c left join transactionmast t on c.custid = t.custid ";

if ($custid >0)
{
    $qry1=$qry1." and c.custid=".$custid;
}

if ($custtype !="0")
{
    $qry1=$qry1." and c.custtype='".$custtype."'";
}

$qry1 =$qry1." group by c.custid,c.custdesc,c.custtype,t.status having IF( (sum(t.debitamt) - sum(t.creditamt)) is NULL,0,(sum(t.debitamt) - sum(t.creditamt)) )  != 0 and t.status = 0 order by sum(t.debitamt) - sum(t.creditamt) desc";

//echo $qry1;
$qry = mysql_query($qry1);
$data = array();
$i=1;
while($rows = mysql_fetch_array($qry))
{
	$data[] = array("Sr_NO" => $i++,
					"custid" => $rows['custid'],
					"custdesc" => $rows['custdesc'],
                    "outstdamt" => $rows['outstdamt'],	
                    "custtype" => $rows['custtype'],
                    "report" =>	sprintf("outprint_report.php?custid=%d&custdesc=%s", $rows['custid'],$rows['custdesc']) 
                    );
}
echo json_encode($data);
?>